import request from "@/utils/request";
// @ts-ignore
import {DeviceModel} from "@/model/DeviceModel";
import {PagingModel} from "@/model/PagingModel";


/**
 * 设备相关API
 */
export default class DeviceApi {

    /**
     * 查询当前系统设备列表
     */
    public static listDevice(searchKey: string, deptId: string, usedType: number = undefined, pageNum: number, pageSize: number): Promise<PagingModel<DeviceModel>> {
        return new Promise((resolve, reject) => {
            request({
                url: '/broadcast/device/list',
                method: 'get',
                params: {
                    searchKey,
                    deptId,
                    usedType: usedType||undefined,
                    pageNum,
                    pageSize
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    const rows: DeviceModel[] = res.rows.map(item => new DeviceModel().deserialize(item))
                    const pageModel = new PagingModel<DeviceModel>().deserialize({
                        data: rows,
                        pageSize: +pageSize,
                        pageIndex: +pageNum,
                        total: +res.total
                    })
                    resolve(pageModel)
                } else {
                    reject(res);
                }
            })
        })
    }

    /**
     * 创建设备
     * @param device
     */
    public static saveDevice(device: DeviceModel): Promise<DeviceModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/device/save`,
                method: 'POST',
                data: device
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new DeviceModel().deserialize(res.data))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    /**
     * 更新字典
     * @param id
     * @param device
     */
    public static updateDevice(id:string, device: DeviceModel): Promise<DeviceModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/device/${id}`,
                method: 'PUT',
                data: device
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new DeviceModel().deserialize(res.data))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    public static deleteDevice(id:string): Promise<boolean> {
        return new Promise<boolean>((resolve, reject) => {
            request({
                url: `/broadcast/device/${id}`,
                method: 'DELETE',
            }).then((res: any) => {
                if (res) {
                    resolve(res)
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    /**
     * 查询未分配诊室的医生终端列表
     */
    public static listFreeDoctorClient(): Promise<DeviceModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: '/queue/device/free/doctor/terminal',
                method: 'get',
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new DeviceModel().deserialize(item)))
                } else {
                    reject(res);
                }
            })
        })
    }
}

